﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Adams.Public.Mobile
{
    public static class Api
    {
        public static T GetData<T>(string url, object data, string format)
        {
            string apiurl = Shared.Config.GetAppSetting("APIUrl");
            apiurl = apiurl.Trim('/');
            url = apiurl + "/" + url.Trim('/');
            string ret = string.Empty;
            if (Shared.Str.IsEqual(format, "json"))
            {
                if (data == null)
                {
                    ret = Shared.Web.GetUrl(url);
                }
                else
                {
                    ret = Shared.Web.GetUrlWithJsonPost(url, data);
                }
                var model = Shared.Serialize.DeserializeJson<T>(ret);
                return model;
            }
            else
            {
                if (data == null)
                {
                    ret = Shared.Web.GetUrlInXml(url);
                }
                else
                {
                    ret = Shared.Web.GetUrlWithXmlPost(url, data);
                }
                var model = Shared.Serialize.DeserializeXml<T>(ret);
                return model;
            }
        }

        public static T GetData<T>(string url)
        {
            return GetData<T>(url, null, "json");
        }

        public static T GetData<T>(string url, object data)
        {
            return GetData<T>(url, data, "json");
        }

        public static T GetData<T>(string url, string format)
        {
            return GetData<T>(url, null, format);
        }
    }
}